//
//  XCNewFeatureController.m
//  彩票
//
//  Created by XC on 16/10/7.
//  Copyright © 2016年 XC. All rights reserved.
//

#import "XCNewFeatureController.h"
#import "XCNewFeatureCell.h"


@interface XCNewFeatureController ()
@property(nonatomic,weak)UIImageView *guideView;
@property(nonatomic,weak)UIImageView *guideLargeText;
@property(nonatomic,weak)UIImageView *guideSmallText;
@property(nonatomic,assign)CGFloat offsetX;
@end

@implementation XCNewFeatureController

static NSString * const reuseIdentifier = @"Cell";

-(instancetype)init
{
    //创建流水布局
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
    
    layout.itemSize=[UIScreen mainScreen].bounds.size;
 
    layout.minimumInteritemSpacing=0;
    
    layout.minimumLineSpacing=0;
    
    
    return [self initWithCollectionViewLayout:layout];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    [UIApplication sharedApplication].statusBarHidden=YES;
    [self.collectionView registerClass:[XCNewFeatureCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    self.collectionView.showsHorizontalScrollIndicator=NO;
    
    self.collectionView.bounces=NO;
    
    self.collectionView.pagingEnabled=YES;
    
    [self setUpAllFeature];
}

-(void)setUpAllFeature
{
    UIImageView *guideView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide1"]];
    [guideView sizeToFit];
    guideView.center=CGPointMake(self.view.width*0.5, self.view.height*0.4);
    [self.collectionView addSubview:guideView];
    _guideView=guideView;
    
    UIImageView *guideLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLine"]];
    
    guideLine.width=guideLine.width*1.15;
    guideLine.x-=25;
    [self.collectionView addSubview:guideLine];
    
    UIImageView *guideLargeText=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLargeText1"]];
    [guideLargeText sizeToFit];
    guideLargeText.center=CGPointMake(self.view.width*0.5, self.view.height*0.7);
    [self.collectionView addSubview:guideLargeText];
    _guideLargeText=guideLargeText;
    
    UIImageView *guideSmallText=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideSmallText1"]];
    [guideSmallText sizeToFit];
    guideSmallText.center=CGPointMake(self.view.width*0.5, self.view.height*0.8);
    [self.collectionView addSubview:guideSmallText];
    _guideSmallText=guideSmallText;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //获取当前偏移量
    CGFloat offsetX=scrollView.contentOffset.x;
    
    //获取偏移量的差值
    CGFloat delta=offsetX-_offsetX;
    
    _guideView.x+=2*delta;
    
    _guideLargeText.x+=2*delta;
    
    _guideSmallText.x+=2*delta;
    
    [UIView animateWithDuration:0.25 animations:^{
        _guideView.x-=delta;
        
        _guideLargeText.x-=delta;
        
        _guideSmallText.x-=delta;
    }];

    int page=offsetX/self.view.width+1;
    
    _guideView.image=[UIImage imageNamed:[NSString stringWithFormat:@"guide%d",page]];
    
    _guideLargeText.image=[UIImage imageNamed:[NSString stringWithFormat:@"guideLargeText%d",page]];
    
    _guideSmallText.image=[UIImage imageNamed:[NSString stringWithFormat:@"guideSmallText%d",page]];
    
    _offsetX=offsetX;
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return 4;
}

- (XCNewFeatureCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    XCNewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
        NSString *imageName=[NSString stringWithFormat:@"guide%ldBackground568h",indexPath.row+1];
        UIImage *image=[UIImage imageNamed:imageName];
    
    cell.image=image;
    
        [cell indexPath:indexPath count:4];

    return cell;
}



@end
